package com.second.app.thread.线程基础.day02;

import java.util.Random;

/**
 * @Author GYZ
 * @Date 2023/11/17
 * <pre>
 *   线程优先级特性：
 *     案例1 继承性：线程的优先级具有继承性，比如A线程启动B线程，则B线程的优先级与A是一样的。
 *     案例2 规则性：高优先级的总体先于低优先级的执行完成。
 *     案例3 证明线程1先被main线程调用就会先执行完成，造成这样的结果完全是因为线程1的优先级是10造成的。
 *   优先级具有随机性：
 *     线程的优先级高则有限执行完run()方法中的任务，这个结果不能说的太肯定。
 *     因为线程的优先级还具有随机性。也就是优先级高的线程不一定每一次都先执行完成。
 * </pre>
 */
public class priority {

    public static void main(String[] args) {

        //案例1
        class Priority2Task extends Thread {
            @Override
            public void run() {
                System.out.println("Priority2Task run priority=" + this.getPriority());
            }
        }
        class Priority1Task extends Thread {
            @Override
            public void run() {
                System.out.println("Priority1Task run priority=" + this.getPriority());
                Priority2Task priority2Task = new Priority2Task();
                priority2Task.start();
            }
        }
//        System.out.println("main thread begin priority=" + Thread.currentThread().getPriority());
//        Thread.currentThread().setPriority(6);
//        System.out.println("main thread end priority=" + Thread.currentThread().getPriority());
//        Priority1Task priority1Task = new Priority1Task();
//        priority1Task.start();
        //案例2
        class MyThread001 extends Thread {
            @Override
            public void run() {
                long beginTime = System.currentTimeMillis();
                long addResult = 0;
                for (int j = 0; j < 10; j++) {
                    for (int i = 0; i < 50000; i++) {
                        Random random = new Random();
                        random.nextInt();
                        addResult = addResult + 1;
                    }
                }
                long endTime = System.currentTimeMillis();
                System.out.println("★★★★★★★★★★★ MyThread001 use time=" + (endTime - beginTime));
            }
        }

        class MyThread002 extends Thread {
            @Override
            public void run() {
                long beginTime = System.currentTimeMillis();
                long addResult = 0;
                for (int j = 0; j < 10; j++) {
                    for (int i = 0; i < 50000; i++) {
                        Random random = new Random();
                        random.nextInt();
                        addResult = addResult + 1;
                    }
                }
                long endTime = System.currentTimeMillis();
                System.out.println("☆☆☆☆☆☆☆☆☆☆☆ MyThread002 use time=" + (endTime - beginTime));
            }
        }

        for (int i=0;i<5;i++){
            MyThread001 myThread001 = new MyThread001();
            myThread001.setPriority(1);
            myThread001.start();
            MyThread002 myThread002 = new MyThread002();
            myThread002.setPriority(10);
            myThread002.start();
        }
    }
}
